package ru.maxmatveev.dyndao.impl.hibernate;

import net.sf.cglib.proxy.Enhancer;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassAdapter;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodAdapter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
/**
 * Factory for getting DAO instances.
 * @author Max Matveev
 *         Date: Dec 20, 2008
 *         Time: 10:35:14 PM
 */
public class DaoFactory {

    /**
     * Get dao instance.
     * @param daoClass Class of desired DAO
     * @param entityClass Entity class used in specified DAO
     * @return DAO instance
     */
    @SuppressWarnings({"unchecked"})
    public static Object getInstance(Class daoClass, Class entityClass) {
        Enhancer enhancer = new Enhancer();
        enhancer.setInterceptDuringConstruction(false);
        DynamicDaoImpl dao = new DynamicDaoImpl(entityClass);
        enhancer.setSuperclass(DynamicDaoImpl.class);
        Class[] interfaces = {daoClass};
        enhancer.setInterfaces(interfaces);
        enhancer.setCallback(dao);
        Class[] constructorArgumentClasses = {Class.class};
        Object[] constructorArgumentValues = {entityClass};
        return enhancer.create(constructorArgumentClasses, constructorArgumentValues);
    }

}
